/*
 * @(#)DateUtils.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.util.date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.bis.framework.util.common.WebXmlSessionTimeOutReader;

/**
 * DateUtils
 * 
 * @author BIS 2012-12-24
 */
public final class DateUtils
{

    /**
     * DATE_FORMAT
     */
    private static final SimpleDateFormat DATE_FORMAT      = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * TIME_FORMAT
     */
    private static final SimpleDateFormat TIME_FORMAT      = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * DATETIME_FORMAT
     */
    private static final SimpleDateFormat DATETIME_FORMAT  = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    /**
     * CHINADATE_FORMAT
     */
    private static final SimpleDateFormat CHINADATE_FORMAT = new SimpleDateFormat("yyyy年MM月dd日");

    /**
     * weekMap
     */
    private static Map<Integer, String>   weekMap          = Collections.synchronizedMap(new HashMap<Integer, String>());

    /**
     * TIME_OUT
     */
    public static final int               TIME_OUT         = WebXmlSessionTimeOutReader.SessionTimeOutReader();
    /**
     * Date
     */
    static
    {
        weekMap.put(1, "星期一");
        weekMap.put(2, "星期二");
        weekMap.put(3, "星期三");
        weekMap.put(4, "星期四");
        weekMap.put(5, "星期五");
        weekMap.put(6, "星期六");
        weekMap.put(7, "星期日");
    }

    /**
     * convertToDateTime
     * @Description:
     * @param date
     * @return
     */
    public static String convertToDateTime(Date date)
    {
        return DATETIME_FORMAT.format(date);
    }

    /**
     * convertToDate
     * @Description:
     * @param date
     * @return
     */
    public static String convertToDate(Date date)
    {
        return DATE_FORMAT.format(date);
    }

    /**
     * convertToTime
     * @Description:
     * @param date
     * @return
     */
    public static String convertToTime(Date date)
    {
        return TIME_FORMAT.format(date);
    }

    /**
     * compare
     * @Description:
     * @param lastLoginDate
     * @return
     */
    public static boolean compare(Date lastLoginDate)
    {
        //1000/s
        Date now = new Date();
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(now);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(lastLoginDate);
        long tt = cal1.getTimeInMillis() - cal2.getTimeInMillis();
        long uu = tt / (1000 * 60);
        if ((tt / (1000 * 60)) >= TIME_OUT)
            return false;
        else
            return true;
    }

    /**
     * dayForWeek
     * @Description:
     * @param pTime
     * @return
     */
    public static String dayForWeek(Date pTime)
    {

        String chinaDate = CHINADATE_FORMAT.format(pTime);
        Calendar c = Calendar.getInstance();
        c.setTime(pTime);
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1)
        {
            dayForWeek = 7;
        }
        else
        {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        return chinaDate + " " + weekMap.get(dayForWeek);
    }

    /**
     * main
     * @Description:
     * @param args
     */
    public static void main(String[] args)
    {
        try
        {

            System.out.println(DateUtils.dayForWeek(new Date()));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
